﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GNSCore.Helpers;
using GNSWebSite.Managers;
using GNSCore.Model;
using GNSCore.Settings;
using GNSCore.Managers;
using GNSCore.Repositories;
using GNSCore.Model.Context;
using GNSCore.Repositories.IOC;


namespace GNSWebSite.MasterPages
{
    public partial class Main : System.Web.UI.MasterPage
    {
        IRepository<GNSCore.Model.Locale> _localeRepository;
        IGNSContext _context;
        protected void Page_Load(object sender, EventArgs e)
        {
            InitDependencies();
            LocaleManager manager = new LocaleManager(_localeRepository);

            if (!IsPostBack)
            {
                ddlLanguages.DataValueField = "Name";
                ddlLanguages.DataTextField = "Language";
                //ddlLanguages.DataSource = GNSCore.Managers.LocaleManager.GetLocales();
                ddlLanguages.DataSource = manager.GetLocales();
                ddlLanguages.DataBind();
                for (int i = 0; i < ddlLanguages.Items.Count; i++)
                {
                    if (ddlLanguages.Items[i].Value ==
                        System.Threading.Thread.CurrentThread.CurrentUICulture.Name)
                    {
                        ddlLanguages.Items[i].Selected = true;
                    }

                }
            }

        }
        private void InitDependencies()
        {
            _localeRepository = IOCManager.IOCContainer.Resolve<IRepository<Locale>>();
            _context = IOCManager.IOCContainer.Resolve<IGNSContext>();
            _localeRepository.SetContext(_context);
        }
        protected void ddlLanguages_SelectedIndexChanged(object sender, EventArgs e)
        {
            GNSWebSite.Helpers.LocaleHelper.SetCurrent(ddlLanguages.SelectedValue);
            Response.Redirect(Request.RawUrl);
        }
        protected void hlLogout_LoggingOut(object sender, LoginCancelEventArgs e)
        {
            System.Web.Security.FormsAuthentication.SignOut();
            Response.Redirect("/Pages/Default.aspx");
        }
    }
}
